package com.esiran.payadmin.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class MapUtilEx {
    private static Gson gson;
    static {
        gson = new GsonBuilder().serializeNulls().create();
    }
    @SuppressWarnings("unchecked")
    public static <T> T getKey(Map<String,Object> map,String key){
        return (T) map.get(key);
    }
    public static String serialize(Map<String,Object> params){
        int i = 0;
        StringBuilder sb = new StringBuilder();
        for (String k: params.keySet()){
            sb.append(String.format("%s=%s",k,String.valueOf(params.get(k))));
            if (i < params.size()-1){
                sb.append("&");
            }
            i++;
        }
        return sb.toString();
    }
    public static String serializes(Map<String,String> params){
        int i = 0;
        StringBuilder sb = new StringBuilder();
        for (String k: params.keySet()){
            String v = params.get(k);
            if (v == null){
                continue;
            }
            sb.append(String.format("%s=%s",k,v));
            if (i < params.size()-1){
                sb.append("&");
            }
            i++;
        }
        return sb.toString();
    }
    public static String serializeUri(Map<String,String> params){
        params.forEach((k,v)->{
            try {
                params.put(k,URLEncoder.encode(v,"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        });
        return serializes(params);
    }
    public static String encodeSerialize(Map<String,Object> params){
        if (params == null){
            return null;
        }
        return EncryptUtil.md5(serialize(params)).toUpperCase();
    }

    public static <T> Wrapper<T> buildOrders(Map<String, Boolean> orders){
        EntityWrapper<T> ew = new EntityWrapper<>();
        if (orders == null){
            return ew;
        }
        for (String key : orders.keySet()){
            ew.orderBy(key,orders.get(key));
        }
        return ew;
    }
    public static <K1,V1,K2,V2> Map<K2,V2> map(Map<K1,V1> map, Function<MapKvEntity<K1,V1>,MapKvEntity<K2,V2>> action){
        Map<K2,V2> newMap = new HashMap<>();
        map.forEach((k,v)->{
            MapKvEntity<K2,V2> kvEntity = action.apply(new MapKvEntity<>(k,v));
            newMap.put(kvEntity.getKey(),kvEntity.getValue());
        });
        return newMap;
    }
    public static class MapKvEntity<K,V>{
        private K key;
        private V value;

        public MapKvEntity(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }
    }

    public static <T> T toEntity(Map<?,?> map,Class<T> tClass){
        String json = gson.toJson(map);
        return gson.fromJson(json,tClass);
    }
    public static String toJson(Map<?,?> map){
        return gson.toJson(map);
    }
}
